﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Common
{

    public static class Utility
    {
        public static string CurrentPath
        {
            get
            {
                return Application.StartupPath;
            }
        }
        /// <summary>
        /// 当前整理的文件夹
        /// </summary>
        public static string FinishPath { get; set; }

        public static string BookPath
        {
            get
            {
                if (!Directory.Exists(String.Format("{0}//Book", CurrentPath)))
                    CreateFolder(String.Format("{0}//Book", CurrentPath));
                return String.Format("{0}//Book", CurrentPath);
            }
        }

        public static string RegFilePath
        {
            get
            {
                return CurrentPath + "\\RegCOC.coc";
            }
        }

        private static void CreateFolder(string p)
        {
            throw new NotImplementedException();
        }

        public static Dictionary<string, string> BookList
        {
            get
            {
                Dictionary<string, string> bookList = new Dictionary<string, string>();
                string[] files = Directory.GetFiles(BookPath);

                foreach (string item in files)
                {
                    bookList.Add(Path.GetFileNameWithoutExtension(item), Path.GetFullPath(item));
                }

                return bookList;
            }
        }

        public static string MachineCodeSec
        {
            get
            {
                return MachineCode.GetCpuInfo() + MachineCode.GetHDid() + MachineCode.GetMoAddress();
            }
        }

        public static string MachineCodeMD5
        {
            get
            {
                return Security.GetMd5_16(MachineCodeSec);
            }
        }

        public static string FileString(string p)
        {
            if (p.IndexOf('\\') == -1)
            {
                p = String.Format("{0}\\{1}", CurrentPath, p);
            }

            if (!File.Exists(p))
            {
                File.Create(p).Close();
                return string.Empty;
            }

            string str = string.Empty;
            using (StreamReader sr = new StreamReader(p, Encoding.Default))
            {
                str = sr.ReadToEnd();
                sr.Close();
            }
            return str;
        }

        public static bool DeleteFile(string p)
        {
            try
            {
                File.Delete(p);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static void WriteFile(string file, string str)
        {
            string fullpath = String.Format("{0}/{1}", CurrentPath, file);

            if (!File.Exists(fullpath)) File.Create(fullpath).Dispose();
            using (StreamWriter sw = new StreamWriter(fullpath, true, Encoding.Default))
            {
                sw.Write(str);
                sw.Dispose();
            }



        }
    }
}
